package com.yrj.shopping;
import java.util.Scanner;

public class ShoppingCart {
    private Goods[] shoppingCart;
    private int capacity=3;


    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public Goods[] getShoppingCart() {
        return shoppingCart;
    }

    public void setShoppingCart(Goods[] shoppingCart) {
        this.shoppingCart = shoppingCart;
    }

    Scanner s=new Scanner(System.in);

    public void addgoods(){

        Goods[] rgoods=new Goods[5];
        Goods apple=new Goods("苹果","水果",5,0);
        Goods banana=new Goods("香蕉","水果",7,0);
        Goods pudding=new Goods("布丁","甜品",10,0);
        Goods tiramisu=new Goods("提拉米苏","甜品",25,0);
        Goods coke=new Goods("可乐","饮品",3,0);
        rgoods[0] = apple;
        rgoods[1] =banana;
        rgoods[2] = pudding;
        rgoods[3] = tiramisu;
        rgoods[4] = coke;
        for(int i=0;i<rgoods.length;i++){
            rgoods[i].getgoods1();
        }

        out:
        for(int j=0;j<capacity;j++){
            System.out.println("请输入您要加入购物车的商品：");
            String input=s.nextLine();
            System.out.println("请输入您加入购物车商品的数量：");
            int incount=s.nextInt();
            s.nextLine();
            for(int i=0;i<5;i++){
                if(input.equals(rgoods[i].getName())){
                    shoppingCart[j]=rgoods[i];
                    shoppingCart[j].setNumber(incount);
                    shoppingCart[j].getgoods2();
                    if(j==2){
                        System.out.println("购物车已满，请输入1删除商品或输入2结束购物，展示购物车");
                        //实现删除后添加物品
                        int operation=s.nextInt();
                        s.nextLine();
                        if(operation==1){
                            removegoods(shoppingCart);
                            j--;
                            break;
                        }
                        if(operation==2){
                            for(int k=0;k<shoppingCart.length;k++){
                                shoppingCart[k].getgoods2();
                            }
                            break out;

                        }
                    }

                }

            }

        }
    }
    public Goods[] removegoods(Goods[] goods){
        System.out.println("请输入您要删除的商品：");
        String input=s.nextLine();
        for(int i=0;i<shoppingCart.length;i++){
            if(input.equals(shoppingCart[i].getName())){
                for(int j=i;j<shoppingCart.length-1;j++){
                    shoppingCart[j]=shoppingCart[j+1];
                }

            }
        }

        Goods[] newgoods=new Goods[goods.length];

        for(int i=0;i<goods.length-1;i++){
            newgoods[i]=goods[i];
        }
        return newgoods;
    }




}
